package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.ui.widget.stickyheaders.model.ItemModel;
import com.afkt.project.ui.widget.stickyheaders.tools.Utils;
import com.afkt.project.ui.widget.stickyheaders.ultimateprovider.EventTransmissionListener;
import com.afkt.project.ui.widget.stickyheaders.ultimateprovider.UltimateProvider;
import com.ohos.afkt.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;

import java.util.List;

public class HeaderListSlice extends BaseActivity implements EventTransmissionListener {
    /**
     * 标题栏
     */
    private DirectionalLayout bar_layout;
    private DirectionalLayout bar;
    private Image slider_return;


    private ListContainer mListContainer;
    private DependentLayout mFloatGroupLayout;
    private List<ItemModel> mModelList;
    private UltimateProvider mUltimateProvider;
    private boolean mHasHeader = false;
    private boolean mIsRemoveItem = false;
    private Text mTvTitle;
    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_header_container_slider1;
    }

    @Override
    protected void initView() {
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_slider_bar_layout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_recycle_slider_bar, null, false);
        bar_layout.addComponent(bar);
        slider_return = (Image) bar.findComponentById(ResourceTable.Id_silder_return);

        mModelList = Utils.demoList();
        mFloatGroupLayout = (DependentLayout) findComponentById(ResourceTable.Id_floatGroupLayout);
        mFloatGroupLayout.setClickedListener(component -> {
            return;
        });
        mTvTitle = (Text) findComponentById(ResourceTable.Id_tv_title);
        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_listView);
        mUltimateProvider = new UltimateProvider<>(mModelList, getContext());
        mUltimateProvider.setEventTransmissionListener(this::onEventTransmission);
        mListContainer.setItemProvider(mUltimateProvider);
        mListContainer.setScrolledListener((component, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (mIsRemoveItem) {
                mIsRemoveItem = false;
                //优化部分场景，防止重复计算
                if (scrollY > oldScrollY) {
                    return;
                }
            }
            int visibleIndex = mListContainer.getFirstVisibleItemPosition();
//                Utils.logi("onContentScrolled visibleIndex = " + visibleIndex);
            if (visibleIndex == 0) {
                Component firstVisibleItemView = mListContainer.getComponentAt(0);
                if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) {
                    Utils.warn("##### 滚动到顶部 ######");
                    mHasHeader = false;
                    setHeaderVisibility(Component.HIDE);
                    return;
                }
            }
            //判断是否存在父控件，存在则显示，不存在继续隐藏
            if (mModelList.size() > 0) {
                mHasHeader = false;
                ItemModel headerModel = null;
                if (mModelList.get(visibleIndex).isHeader()) {
                    mHasHeader = true;
                    headerModel = mModelList.get(visibleIndex);
                } else {
                    for (int j = visibleIndex - 1; j >= 0; j--) {
                        if (mModelList.get(j).isHeader()) {
                            mHasHeader = true;
                            headerModel = mModelList.get(j);
                            break;
                        }
                    }
                }

                if (mHasHeader) {
                    String title = headerModel.getTitle();
                    int[] padding = headerModel.getPaddings();
                    getUITaskDispatcher().asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            mTvTitle.setText(title);
                            //TODO Text控件存在bug，值设置了，显示的不对
//                                Utils.debug("onContentScrolled title = " + title + ", getText = " + mTvTitle.getText());
                            mTvTitle.setPadding(padding[0], padding[1], padding[2], padding[3]);
                            setHeaderVisibility(Component.VISIBLE);
                        }
                    });

                } else {
                    setHeaderVisibility(Component.HIDE);
                }
            }
        });
        //要放在初始后
        if (mModelList.size() > 0) {
            mTvTitle.setText(mModelList.get(0).getTitle());
            ShapeElement element = new ShapeElement();
            RgbColor rgbColor = new RgbColor(0, 255, 255);
            element.setRgbColor(rgbColor);
            mFloatGroupLayout.setBackground(element);
        }
    }
    public void setHeaderVisibility(int visibility) {
        if (mFloatGroupLayout.getVisibility() != visibility) {
            mFloatGroupLayout.setVisibility(visibility);
        }
    }
    @Override
    protected void initListener() {
        slider_return.setClickedListener(component -> onBackPressed());
    }

    @Override
    protected void onBackground() {
        bar_layout.removeComponent(bar);
        super.onBackground();
    }

    @Override
    public Object onEventTransmission(Object target, Object params, int eventId, CallBack callBack) {
        mIsRemoveItem = true;
        mModelList.remove((int) params);
        mUltimateProvider.setModels(mModelList);
        return null;
    }
}
